package com.ztom.offer.v2;

/**
 * 二进制中 1 的个数
 * <p>
 * https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/
 *
 * @author ZhangTao
 */
public class Code14HammingWeight {

    public int hammingWeight1(long n) {
        if (n == 0) {
            return 0;
        }

        int count = 0;
        while (n != 0) {
            n -= n & -n;
            count++;
        }
        return count;
    }

    public int hammingWeight(long n) {
        if (n == 0) {
            return 0;
        }

        int count = 0;
        while (n != 0) {
            n &= n - 1;
            count++;
        }
        return count;
    }
}
